package com.lovo.service.impl;

import com.lovo.dao.UserDAO;
import com.lovo.dao.impl.UserDAOImpl;
import com.lovo.pojo.UserDO;
import com.lovo.service.UserService;

import javax.swing.*;

public class UserServiceImpl implements UserService {
    private UserDAO userDAO = new UserDAOImpl();
    private UserDAOImpl usersDAO = new UserDAOImpl();

    @Override
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        return userDAO.getByUsernameAndPwd(username, pwd);
    }

    @Override
    public void saveMoney(int id, double money) {
        UserDO user = userDAO.getById(id);
        user.setMoney(user.getMoney() + money);
        userDAO.update(user);
    }

    @Override
    public void getMoney(int id, double money) throws Exception {
        UserDO user = userDAO.getById(id);
        if (user.getMoney() < money) {
            throw new Exception("余额不足");
        }
        user.setMoney(user.getMoney() - money);
        userDAO.update(user);
    }

    @Override
    public String TransferMoney(int id, String name, double money) {
        UserDO loginuser = userDAO.getById(id);
        UserDO otheruser = userDAO.getByUsername(name);
        if (name.equals(loginuser.getUsername())) {
            return "不能转给自己";
        }
        if (otheruser == null) {
            return "差无此人";
        }
        if (loginuser.getMoney() < money) {
            return "余额不足";
        }
        loginuser.setMoney(loginuser.getMoney() - money);
        otheruser.setMoney(otheruser.getMoney() + money);
        try {
            usersDAO.getConnection();
            usersDAO.setAutoCommit(false);
            userDAO.update(loginuser);
            userDAO.update(otheruser);
            usersDAO.commit();
        } catch (Exception e) {
            usersDAO.rollback();
            return "转账失败";
        } finally {
            usersDAO.close();
        }
        return "转账成功";
    }

    @Override
    public String ModifyMoney(int id, String oldpwd, String newpwd, String newpwd1) {
        UserDO user = userDAO.getById(id);
        if (oldpwd.equals(user.getUserpsw())) {
            if (newpwd.equals(newpwd1)) {
                if (newpwd.matches("^[a-zA-Z]\\w{5,17}$")) {
                    user.setUserpsw(newpwd);
                    usersDAO.update(user);
                    return "修改密码成功，请重新登录";
                } else {
                    return "密码格式不对";
                }
            } else {
                return "两次密码不匹配";
            }
        } else {
            return "与原密码不匹配";
        }
    }

    @Override
    public String Zhuce(String username, String pwd, double money) {
        if (username.matches("^[a-zA-Z][a-zA-Z0-9_]{4,15}$")) {
            if (pwd.matches("^[a-zA-Z]\\w{5,17}$")) {
                UserDO userDO = new UserDO(username, pwd, money);
                usersDAO.insert(userDO);
                return "注册成功";
            } else {
                return "密码格式不匹配";
            }
        } else {
            return "用户名格式不匹配";
        }
    }
}
